Skip to content

Conversation

@grandima
Copy link

@grandima grandima commented Jun 1, 2025

Linked Issues/PRs

close #2657

Description

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests
  • The specification matches the implemented behavior (link update PR if changes are needed)

Before requesting review

  • I have reviewed the code myself
  • I have created follow-up issues caused by this PR and linked them here

After merging, notify other teams

[Add or remove entries as needed]


Note

Replaces owned HexString with Cow-backed HexString<'_> and updates GraphQL types/methods to borrow bytes where possible, reducing clones and adding required lifetimes.

  • Scalars
    • Replace HexString with HexString<'a> backed by Cow<[u8]>.
    • Add conversions: From<Vec<u8>>, From<&[u8]>, From<HexString> for Vec<u8>; update Display, ScalarType, CursorType, FromStr, and Nonce conversions to support borrowing.
  • GraphQL Schema Updates
    • Change return/input types from HexString to HexString<'_> across schema: blob, contract, da_compressed, message, storage, tx (queries, mutations, subscriptions, types, receipts), and upgrades.
    • Introduce lifetimes in types and unions where needed (e.g., Input<'a>, InputCoin<'a>, InputMessage<'a>, UploadedBytecode<'a>, subscriptions).
    • Construct HexString via borrowing (HexString::from(&[..]) / .into()) and remove unnecessary clones in mappers (e.g., witnesses, scripts, predicate/data, storage slots, block/tx streams).
    • Adjust pagination and connection cursors to use HexString<'_> where relevant.

Written by Cursor Bugbot for commit ae7848d. This will update automatically on new commits. Configure here.


#[derive(Clone, Debug, derive_more::Into, derive_more::From, PartialEq, Eq)]
pub struct HexString(pub(crate) Vec<u8>);
pub struct HexString(pub(crate) Cow<'static, [u8]>);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we want a static lifetime here, can we use

Suggested change
pub struct HexString(pub(crate) Cow<'static, [u8]>);
pub struct HexString<'a>(pub(crate) Cow<'a, [u8]>);

instead?

Copy link
Author

@grandima grandima Jun 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rymnc I've changed implementation to use 'a instead of 'static. Please review.

@grandima grandima closed this Jun 13, 2025
@grandima grandima deleted the 2657-scalars-HexString-cow branch June 13, 2025 11:49
@grandima grandima restored the 2657-scalars-HexString-cow branch June 15, 2025 14:43
@grandima grandima reopened this Jun 15, 2025
@grandima grandima requested a review from AurelienFT as a code owner June 15, 2025 14:44
@grandima grandima force-pushed the 2657-scalars-HexString-cow branch 2 times, most recently from 7a9aec6 to a571b5f Compare June 16, 2025 17:10
@grandima
Copy link
Author

grandima commented Jul 6, 2025

@rymnc I applied the suggested changes. Please review.

@grandima grandima requested a review from rymnc July 8, 2025 17:30
@grandima grandima force-pushed the 2657-scalars-HexString-cow branch from 5dcdbf0 to 06150af Compare November 2, 2025 01:15
@grandima grandima force-pushed the 2657-scalars-HexString-cow branch from 06150af to ae7848d Compare November 2, 2025 14:51
// TODO: Avoid cloning the bytes here.
// https://github.com/FuelLabs/fuel-core/issues/2657
HexString(bytes.as_ref().clone())
HexString::from(bytes.as_ref().clone())
Copy link
Author

@grandima grandima Nov 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cow::Owned is still needed here and as a result - cloning. Because Cow::Borrowed cannot be returned since it references only to a data that is owned by this map. Here's a compilation failing example if we use Cow::Borrowed:

error[E0515]: cannot return value referencing function parameter `bytes`
   --> crates/fuel-core/src/schema/block.rs:443:21
    |
443 |                     HexString::from(bytes.as_ref().as_slice())
    |                     ^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^
    |                     |               |
    |                     |               `bytes` is borrowed here
    |                     returns a value referencing data owned by the current function

@grandima
Copy link
Author

grandima commented Nov 2, 2025

hey @rymnc please check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid cloning of vectors in HexString

2 participants